import axios from 'axios';
import { ElMessage } from 'element-plus'
import router from '@/router'

const baseURL = 'http://localhost:8080';
const instance = axios.create({ 
    baseURL,
    withCredentials: true // 确保请求携带 cookie
})


//响应拦截器
instance.interceptors.response.use(
    result => {
        //判断业务状态码
        if(result.data.code===1){
            return result.data;
        }

        ElMessage.error(result.data.message?result.data.message:'服务异常')
        return Promise.reject(result.data)
        
    },
    err => {
        if(err.response.status===401){
            ElMessage.error('请先登录')
            router.push('/login')
        }else{
            ElMessage.error('服务异常')
        }
       
        return Promise.reject(err);
    }
)

export default instance;